package com.example.demo.mvvm.ui.vm.fragment;

import android.content.Intent;
import android.widget.Toast;

import com.example.demo.mvvm.BR;
import com.example.demo.mvvm.R;
import com.example.demo.mvvm.bean.KnowBean;
import com.example.demo.mvvm.net.BaseObserver;
import com.example.demo.mvvm.net.NetRequest;
import com.example.demo.mvvm.ui.activity.ChildrenActivity;
import com.example.demo.mvvm.ui.base.BaseRecyclerViewVM;
import com.example.demo.mvvm.utils.ActivityUtils;
import com.example.demo.mvvm.utils.OnItemClickListener;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.List;

import me.tatarka.bindingcollectionadapter2.ItemBinding;

/**
 * Created by 80711 on 2018/5/18.
 */

public class KnowFragmentVm extends BaseRecyclerViewVM<KnowBean> {

    public final ItemBinding<KnowBean> itemBinding = ItemBinding.<KnowBean>of(BR.item, R.layout.item_main_know).bindExtra(BR.listener, new OnItemClickListener<KnowBean>() {
        @Override
        public void onItemClick(int viewId, KnowBean data) {
            ActivityUtils.push(ActivityUtils.peek() , ChildrenActivity.class , new Intent().putExtra("knowBean" , data));
            //Toast.makeText(ActivityUtils.peek(), data.getName(), Toast.LENGTH_SHORT).show();
        }
    });

    public KnowFragmentVm(){
        listener.set(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {

            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                request_data(refreshlayout);
            }
        });


    }

    public void request_data(RefreshLayout refreshlayout){
        NetRequest.getKnowInfo(new BaseObserver<List<KnowBean>>(ActivityUtils.peek() ,refreshlayout,true ) {
            @Override
            public void onSuccess(List<KnowBean> knowBeans, RefreshLayout refreshLayout) {
                refreshLayout.finishRefresh(true);
                items.clear();
                items.addAll(knowBeans);
            }

            @Override
            public void onFailure(int code, String message, RefreshLayout refreshLayout) {

            }
        });
    }

}
